define([],function(){   
    var webViewObj = {
        description:"Webview窗口对象",
        viewID:"webView标识",
        url:"加载的HTML页面地址",
        styles:"窗口的样式",
        viewTitle:"HTML页面的标题",
        parentViewID:"父视图标识",
        childrens:"子视图",

        getURL:function(){
            var me = this;
            return me.url;
        },

        show:function(aniShow, duration, showedCB){
            var me = this;
            cordova.exec(function(){
                showedCB();
            },function(error){
              console.log(error);
            },"WebView","showWebView",[me.viewID,aniShow,duration]);
        },
       
       close:function(aniClose, duration){
            var me = this;
            var ID = me.viewID;
            cordova.exec(function(){
             
                            bsl.webview.removeView(ID);
                            
                            },function(error){
                            console.log(error);
                            },"WebView","closeWebView",[ID,aniClose,duration]);
       },

        init:function(url,id,styles){
            var obj = new Object();
            var me = this;

            obj.viewID = id;  
            obj.url = url
            obj.styles = styles;
            obj.description = me.description;
            obj.parentViewID = me.parentViewID;
            me.setupFunctions(obj);
       
           //去除同ID下的监听事件
           if (window.Listener && window.Listener != 'undefined'){
//               alert(JSON.stringify(window.Listener));
               window.Listener[id] = null;
//               alert(JSON.stringify(window.Listener));
           }
       
            return obj;
        },

        //原生触发监听事件
        triggerEvent:function(type){
            console.log("type:" + type);
            var me = this;
            var Id = me.viewID;
            var listeners = window.Listener[Id];
            if (listeners) {
                var triggerArr = listeners[type];    
                // console.log("arr:" + triggerArr);
                if (triggerArr.length) {
                    for (var i=0; i<triggerArr.length;i++){
                        triggerArr[i]();
                    }
                }
            }
        },  

        //添加事件监听器，event有"close","loaded","loading","error"
        addEventListener:function(event, listener, capture){

            function add(el,type,fn,capture){
                var Id = el.viewID;
                var allListeners = window.Listener;
                allListeners = allListeners || {};
       
                var listeners = allListeners[Id];
                listeners = listeners || {};
                listeners[type] = listeners[type] || [];
                listeners[type].push(fn);
       
               allListeners[Id] = listeners;
                window.Listener = allListeners;
            }

            add(this,event,listener,capture);
//            alert(JSON.stringify(window.Listener))
        },
       
       //移除Webview窗口事件监听器
       removeEventListener:function(event, listener){
           var me = this;
           var ID = me.viewID;
           var allListeners = window.Listener;
           if (allListeners == undefined){
               return;
           }
       
           var listeners = allListeners[ID];
           if (listeners == undefined || !listeners.length){
               return;
           }
       
           var events = listeners[event];
           for (var i=0; i<events.length; i++){
               var func = events[i];
               if (func == listener){
                   events.splice(i,1);
                   break;
               }
           }
           
           allListeners[ID] = listeners;
           window.Listener = allListeners;
       },
       
        //设置Webview窗口的下拉刷新效果
        setPullToRefresh:function(style, refreshCB){
            var me = this;
            var command = [me.viewID,style];
            var refreshs = window.onRefresh;
            refreshs = refreshs || {};
            refreshs[me.viewID] = refreshs[me.viewID] || [];
            refreshs[me.viewID] = refreshCB;
            window.onRefresh = refreshs;//记住回调时的方法
       
            cordova.exec(function(){
                         console.log('setPullToRefresh success');
                    },function(){
                         console.log('setPullToRefresh fail');
                    },"WebView","setPullToRefresh",command);
        },
       
        //告诉原生前端已经请求完数据更新完UI，可以取消下拉刷新的动画了
        endPullToRefresh:function(){
            cordova.exec(function(){
                    console.log('endPullToRefresh success');
                    },function(){
                    console.log('endPullToRefresh fail');
                    },"WebView","endPullToRefresh",[this.viewID]);
        },
       
        //在Webview窗口中添加子窗口
        append:function(webview){
           var me = this;
           var view1ID = me.viewID;
           var view2ID = webview.viewID;
           cordova.exec(function(){
                        var view1 = bsl.webview.getWebviewById(view1ID);
//                        var view2 = bsl.webview.getWebviewById(view2ID);
                        
                        view1.childrens = view1.childrens || [];
                        view1.childrens.push(webview);
                        webview.parentViewID = view1ID;
                        
                        window.Listener[view2ID] = null;
                        bsl.webview.removeView(view2ID);
                        bsl.webview.saveViewToLocalStorage(view1);
                        
                        console.log('append success');
                        },function(){
                        console.log('append fail');
                        },"WebView","appendWebView",[view1ID,view2ID]);
        },
       
       //移除子Webview窗口
       remove:function(webview){
           var me = this;
           var view1ID = me.viewID;
           var view2ID = webview.viewID;
       
           cordova.exec(function(){
                            var view1 = bsl.webview.getWebviewById(view1ID);
                            var shouldDelete = false;
                            var index = 0;
                            for (;index<view1.childrens.length;index++){
                                var view = view1.childrens[index];
                                if (view.viewID == view2ID){
                                    shouldDelete = true;
                                    break;
                                }
                            }
                        
                            if (shouldDelete == true){
                                view1.childrens.splice(index,1);
                                bsl.webview.saveViewToLocalStorage(view1);
                            }
                        
                        },function(){
                            console.log('remove fail');
                        },"WebView","removeWebView",[view1ID,view2ID]);
       },
       
       //从父窗口中移除
       removeFromParent:function(){
           var me = this;
           var parent = me.parent();
       
           if (parent){
               parent.remove(me);
           }
       },
       
       //加载新URL页面
       loadURL:function(url){
           cordova.exec(function(){console.log('loadURL success');},function(){console.log('loadURL fail');},"WebView","loadURL",[this.viewID,url]);
       },
       
       //获取当前Webview窗口的父窗口
       parent:function(){
           var me = this;
           var parentView = bsl.webview.getWebviewById(me.parentViewID);
           return parentView;
       },
       
       //获取Webview窗口的所有子Webview窗口
       children:function(){
           var me = this;
           for (var i=0; i<me.childrens.length; i++){
               var view = me.childrens[i];
               me.setupFunctions(view);
           }
       
           me.childrens = me.childrens || [];
           return me.childrens;
       },
       
       //后退到上次加载的页面
       back:function(){
            var id = this.viewID;
            cordova.exec(function(){
                        console.log('back success');
                         },function(){
                         console.log('back fail');
                         },"WebView","back",[id]);
       },
       
       //查询Webview窗口是否可后退
       canBack:function(callback){
            var id  = this.viewID;
            cordova.exec(function(canBack){
                        var e = {};
                        e.canBack = canBack;
                        callback(e);
                    },function(){
                         
                    },"WebView","canBack",[id]);
       },
       
       //前进到上次加载的页面
       forward:function(){
       
           var id = this.viewID;
           cordova.exec(function(){
                        console.log('forward success');
                        },function(){
                        console.log('forward fail');
                        },"WebView","forward",[id]);
       },
       
       //查询Webview窗口是否可前进
       canForward:function(callback){
       var id  = this.viewID;
       cordova.exec(function(canForward){
                    var e = {};
                    e.canForward = canForward;
                    callback(e);
                    },function(){
                    
                    },"WebView","canForward",[id]);
       },
       
       //给obj加上方法
       setupFunctions:function(obj){
       var me = this;
       obj.getURL = me.getURL;
       obj.show = me.show;
       obj.addEventListener = me.addEventListener;
       obj.triggerEvent = me.triggerEvent;
       obj.setPullToRefresh = me.setPullToRefresh;
       obj.endPullToRefresh = me.endPullToRefresh;
       obj.append = me.append;
       obj.loadURL = me.loadURL;
       obj.parent = me.parent;
       obj.children = me.children;
       obj.back = me.back;
       obj.setupFunctions = me.setupFunctions;
       obj.canBack = me.canBack;
       obj.remove = me.remove;
       obj.close = me.close;
       obj.removeFromParent = me.removeFromParent;
       obj.removeEventListener = me.removeEventListener;
       },
    };

    return webViewObj;

});
